package com.gm.wholesale.module.order.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.gm.commonlib.base.baseAdapter.CommonAdapter;
import com.gm.commonlib.base.baseAdapter.ViewHolder;
import com.gm.commonlib.util.numberUtils.NumberUtils;
import com.gm.wholesale.R;
import com.gm.wholesale.constant.Unit;
import com.gm.wholesale.module.order.bean.AddWeightBean;
import com.gm.wholesale.util.Utils;

import java.util.ArrayList;
import java.util.List;

public class AddWeightView extends LinearLayout {

    private Context mContext;
    private TextView totalTv;
    private ListView listView;
    private List<AddWeightBean> weights= new ArrayList<>();
    public AddWeightView(Context context) {
        super(context);
        initView(context);

    }

    public AddWeightView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(Context context) {

        mContext=context;
        LayoutInflater.from(context).inflate(R.layout.view_add_weight, this);
        totalTv = findViewById(R.id.totalTv);
        listView = findViewById(R.id.listView);
    }



    public void reset() {
        weights.clear();
        totalTv.setText("");
        setVisibility(GONE);
    }

    public List<AddWeightBean> getAddWeight(){
        if (weights.isEmpty()){
            return null;
        }
        return weights;
    }

    public void showView(List<AddWeightBean> weights){
        reset();
        if (weights!=null && !weights.isEmpty()){
            this.weights.addAll(weights);
            updateView();
            showTotal();
            setVisibility(VISIBLE);
        }
    }

    public void showView(AddWeightBean addWeightBean) {
        setVisibility(VISIBLE);
        weights.add(addWeightBean);
        showTotal();
        updateView();
    }

    public double getTotalWeight(){
        double weight=0;
        for (AddWeightBean w:weights){
            weight=NumberUtils.addDouble(w.getWeight(),weight);
        }
        return weight;
    }

    private void showTotal() {
        if (weights.isEmpty()) {
            totalTv.setText("");
            return;
        }
        double total = 0;
        for (AddWeightBean data : weights) {
            total = NumberUtils.addDouble(data.getWeight(), total);
        }
        String unit=weights.get(0).getUnit();
        totalTv.setText(NumberUtils.subZeroAndDot(NumberUtils.getMoneyString(total))+unit);
    }

    public void updateByChangeUnit(String unit){
        if (!Utils.isWeightUnit(unit)){
            reset();
            setVisibility(GONE);
            return;
        }
        if (weights.isEmpty())return;
        String currentUnit=weights.get(0).getUnit();

        if (Utils.isWeightUnit(unit) && Utils.isWeightUnit(currentUnit)){
            if (unit.equals(currentUnit))return;
            for (AddWeightBean bean:weights){
                bean.setWeight(Unit.getWeight(currentUnit,unit,bean.getWeight()));
                bean.setUnit(unit);
            }
            if (adapter!=null){
                showTotal();
                adapter.notifyDataSetChanged();
            }
        }
    }

    private CommonAdapter adapter;

    public void updateView() {
        if (adapter == null) {
            adapter = new CommonAdapter<>(mContext, weights, R.layout.list_add_weight_item) {
                @Override
                public void convert(ViewHolder viewHolder, AddWeightBean data) {
                    viewHolder.setText(R.id.goodWeightTv, NumberUtils.subZeroAndDot(NumberUtils.getMoneyString(data.getWeight())));
                    viewHolder.getView(R.id.deleteWeightImg).setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            weights.remove(data);
                            showTotal();
                            notifyDataSetChanged();
                            if (weights.isEmpty()){
                                setVisibility(GONE);
                                reset();
                            }
                        }
                    });
                }
            };
            listView.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }
    }

}
